<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/add.css"/>
    <script src="../../layer/jquery2.1.4.js"></script>
    <script src="../../layer/layer.js"></script>
</head>
<body>
<div id="app">
    <div class="header">
        <span>修改用户</span>
    </div>
    <div class="content">
        <table>
            <tr>
                <td class="text-right">手机号码</td>
                <td class="content_right">
                    <input class="input inline-input" placeholder="请输入手机号码">
                    <span id="select" class="btn btn-info">立即查找</span>
                </td>
            </tr>
            <tr>
                <td class="text-right"></td>
                <td class="content_right">
                    <span id="userPhone"></span>
                </td>
            </tr>
        </table>
    </div>


    <div class="header">
        <span>查找信息如下</span>
    </div>
    <div class="content">
        <table>
            <tr>
                <td class="text-right">用户名</td>
                <td class="content_right">
                    <input class="input" placeholder="请输入用户名">
                    <span  id="userName"></span>
                </td>

            </tr>
            <tr>
                <td class="text-right">手机号码</td>
                <td class="content_right">
                    <input class="input" placeholder="请输入手机号码">
                    <span id="userPhone1"></span>
                </td>
            </tr>
            <tr>
                <td class="text-right">身份证号码</td>
                <td class="content_right"><input class="input" placeholder="请输入身份证号码"><span id="IDCard"></span></td>
            </tr>
            <tr>
                <td class="text-right">密码</td>
                <td class="content_right"><input class="input" placeholder="请输入密码"><span id="password"></span></td>
            </tr>
            <tr>
                <td class="text-right">性别</td>
                <td class="content_right">
                    <input type="radio" class="status" name="status" value="1">男
                    <input name="status" class="status" type="radio" value="0">女
                </td>
            </tr>
            <tr>
                <td></td>
                <td class="content_right"><span id="update" class="btn">立即修改</span> <span id="reset" class="btn">重置</span></td>
            </tr>
        </table>
    </div>
</div>
</body>
<script>
    $(function () {
        $(".header:eq(1), .content:eq(1)").hide();
        var userId;
        //通过手机号查询
        $("#select").click(function () {
            //验证手机号
            let userPhone = $("input:eq(0)").val();
            if (!(/^^1[0-9]{10}$$/).test(userPhone)&&userPhone != null) {
                $("#userPhone").html("号码格式不正确").css("color", "red");
                return false;
            } else {
                $("#userPhone").html("").css("color", "green");
            }
            let load = layer.load(0);
            $.getJSON("/user/select.do", {userPhone: userPhone}, function (data) {
                layer.close(load);
                if (data.status === 0) {
                    alert(data.result);
                    location.assign("update.jsp");
                } else if (data.status === 1) {
                    $(".header:eq(1), .content:eq(1)").fadeIn(500);
                    //{"status":1,"result":"查询成功","object":{"userId":3,"userName":"zhang","IDCard":"","userGender":1,"password":"shz1989","userPhone":"13385890730"}}
                    userId = data.object.userId;
                    $("input:eq(1)").val(data.object.userName);
                    $("input:eq(2)").val(data.object.userPhone);
                    $("input:eq(3)").val(data.object.IDCard);
                    $("input:eq(4)").val(data.object.password);
                    if (data.object.userGender === 1){
                        $(".status:eq(0)").prop("checked",true);
                    }else {
                        $(".status:eq(1)").prop("checked",true);
                    }
                }
            });
        });
        //修改
        $("#update").click(function () {
            let userName = $("input:eq(1)").val();
            let userPhone = $("input:eq(2)").val();
            if (!(/^^1[0-9]{10}$$/).test(userPhone)) {
                $("#userPhone1").html("号码格式不正确").css("color", "red");
                return false;
            } else {
                $("#userPhone1").html("手机号可用").css("color", "green");
            }
            let IDCard = $("input:eq(3)").val();
            let arg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
            if (IDCard.length == 18 && arg.test(IDCard)) {
                $("#IDCard").html("身份证号可用").css("color", "green");
            } else {
                $("#IDCard").html("身份证号码不正确").css("color", "red");
                return false;
            }
            let password = $("input:eq(4)").val();
            if (!(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/).test(password)) {
                $("#password").html("密码最少6——20位，密码至少包含数字和英文").css("color", "red");
                return false;
            } else {
                $("#password").html("密码可用").css("color", "green");
            }
            let userGender = $(".status:checked").val();
            let load = layer.load(0);
            $.post("/user/update.do",{
                userId:userId,
                userName: userName,
                userGender: userGender,
                userPhone: userPhone,
                IDCard: IDCard,
                password: password
            },function (data) {
                layer.close(load);
                alert(data.result);
                if (data.status === 1){

                    setTimeout(function () {
                        location.assign("update.jsp");
                    },1000);
                }

            },"JSON");
        });
        //重置
        $("#reset").click(function () {
            location.assign("update.jsp");
        });
    });
</script>
</html>
